package com.cqeec.bootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqeec.bootdemo.common.BaseContext;
import com.cqeec.bootdemo.entity.AddressBook;
import com.cqeec.bootdemo.entity.OrderDetail;
import com.cqeec.bootdemo.entity.Orders;
import com.cqeec.bootdemo.entity.ShoppingCart;
import com.cqeec.bootdemo.mapper.OrdersMapper;
import com.cqeec.bootdemo.service.AddressBookService;
import com.cqeec.bootdemo.service.OrderDetailService;
import com.cqeec.bootdemo.service.OrdersService;
import com.cqeec.bootdemo.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Service
public class OrdersServiceImp extends ServiceImpl<OrdersMapper, Orders>
        implements OrdersService {
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    AddressBookService addressBookService;

    @Override
    @Transactional
    public void submit(Orders orders) {
        //需要添加两张表的数据，删除购物车数据
        //部分orders字段
        //1.查询用户购物车的数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        System.out.println(shoppingCartList);
        //准备orders表的数据
        orders.setUserId(BaseContext.getCurrentId());
        orders.setStatus(2);
        orders.setAddressBookId(orders.getAddressBookId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPayMethod(orders.getPayMethod());
        if (orders.getRemark() != null) {
            orders.setRemark(orders.getRemark());
        }
        //计算购物车的总金额
        BigDecimal a = new BigDecimal(0);
        for (ShoppingCart shoppingCart : shoppingCartList) {
            BigDecimal b = new BigDecimal(0);
            BigDecimal number = BigDecimal.valueOf(shoppingCart.getNumber());
            b = number.multiply(shoppingCart.getAmount());
            a=a.add(b);
        }
        orders.setAmount(a);
        //可以去找到地址相关的数据
        //新增数据到orders数据库
        orders.setNumber(String.valueOf(new Random().nextInt(999999999)));
        //添加手机号，地址，用户名
        AddressBook addressBook=addressBookService.getById(orders.getAddressBookId());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(addressBook.getConsignee());
        this.save(orders);
        //2.准备orderDetail表的数据
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orders.getId());
            if (shoppingCart.getDishId() != null) {
                orderDetail.setDishId(shoppingCart.getDishId());
                orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            } else {
                orderDetail.setSetmealId(shoppingCart.getSetmealId());
            }
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetailService.save(orderDetail);
        }
        shoppingCartService.remove(queryWrapper);
    }
}
